import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * 通讯录应用的主类，继承自JFrame类。
 */
public class AddressBook extends JFrame {
    // 存储联系人信息的映射
    private Map<String, String> addressBook;

    // GUI组件
    private JTextField nameField;
    private JTextField phoneNumberField;
    private JTextField deleteNameField;
    private JTextField searchNameField; // 新增文本行用于查找
    private JTextArea displayArea;

    /**
     * 主类的构造方法，用于初始化通讯录应用界面。
     */
    public AddressBook() {
        // 初始化通讯录映射
        addressBook = new HashMap<>();

        // 创建GUI组件
        JLabel nameLabel = new JLabel("姓名:");
        JLabel phoneNumberLabel = new JLabel("电话号码:");
        JLabel deleteLabel = new JLabel("要删除的姓名:");
        JLabel searchLabel = new JLabel("要查找的姓名:"); // 新增标签

        nameField = new JTextField(20);
        phoneNumberField = new JTextField(20);
        deleteNameField = new JTextField(20);
        searchNameField = new JTextField(20); // 新增文本行用于查找

        JButton addButton = new JButton("添加联系人");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addContact();
            }
        });

        JButton deleteButton = new JButton("删除联系人");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteContact();
            }
        });

        JButton searchButton = new JButton("查找联系人");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                searchContact();
            }
        });

        displayArea = new JTextArea(10, 40);
        displayArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(displayArea);

        // 设置布局
        setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel(new GridLayout(5, 2, 5, 5));
        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(phoneNumberLabel);
        inputPanel.add(phoneNumberField);
        inputPanel.add(deleteLabel);
        inputPanel.add(deleteNameField);
        inputPanel.add(searchLabel); // 新增标签
        inputPanel.add(searchNameField); // 新增文本行
        inputPanel.add(new JLabel());
        inputPanel.add(new JLabel());

        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(searchButton);

        // 将文本区域放在一个滚动窗格中
        JPanel displayPanel = new JPanel(new BorderLayout());
        displayPanel.add(scrollPane, BorderLayout.CENTER);

        // 添加面板到窗口
        add(inputPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);
        add(displayPanel, BorderLayout.SOUTH);

        // 设置窗口属性
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 400);
        setTitle("通讯录应用");
        setLocationRelativeTo(null); // 居中显示
        setVisible(true);
    }

    /**
     * 添加联系人的方法，通过用户输入的姓名和电话号码添加新的联系人。
     */
    private void addContact() {
        String name = nameField.getText();
        String phoneNumber = phoneNumberField.getText();

        if (!name.isEmpty() && !phoneNumber.isEmpty()) {
            addressBook.put(name, phoneNumber);
            displayAddressBook();
        } else {
            JOptionPane.showMessageDialog(this, "请输入姓名和电话号码", "输入错误", JOptionPane.ERROR_MESSAGE);
        }

        // 清空输入框
        nameField.setText("");
        phoneNumberField.setText("");
    }

    /**
     * 删除联系人的方法，根据用户输入的姓名删除相应的联系人。
     */
    private void deleteContact() {
        String deleteName = deleteNameField.getText();

        if (!deleteName.isEmpty()) {
            if (addressBook.containsKey(deleteName)) {
                addressBook.remove(deleteName);
                displayAddressBook();
            } else {
                JOptionPane.showMessageDialog(this, "找不到联系人: " + deleteName, "删除失败", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "请输入要删除的联系人姓名", "输入错误", JOptionPane.ERROR_MESSAGE);
        }

        // 清空输入框
        deleteNameField.setText("");
    }

    /**
     * 查找联系人的方法，根据用户输入的姓名查找相应的联系人并显示其电话号码。
     */
    private void searchContact() {
        String searchName = searchNameField.getText();

        if (!searchName.isEmpty()) {
            String phoneNumber = addressBook.get(searchName);
            if (phoneNumber != null) {
                JOptionPane.showMessageDialog(this, "姓名: " + searchName + "\n电话号码: " + phoneNumber, "查询结果", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "找不到联系人: " + searchName, "查询结果", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "请输入要查找的联系人姓名", "输入错误", JOptionPane.ERROR_MESSAGE);
        }

        // 清空输入框
        searchNameField.setText("");
    }

    /**
     * 显示通讯录的方法，将通讯录中的所有联系人信息显示在文本区域中。
     */
    private void displayAddressBook() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : addressBook.entrySet()) {
            sb.append("姓名: ").append(entry.getKey()).append("\t电话号码: ").append(entry.getValue()).append("\n");
        }
        displayArea.setText(sb.toString());
    }

    /**
     * 主方法，启动通讯录应用。
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AddressBook();
            }
        });
    }
}
